<?php

/** 
 * @author Administrator
 * 
 */
class Dev_log_mdl extends MY_Model {
	
	/**
	 */
	public function __construct() {
		parent::__construct ();
		$this->_table = 'hs_dev_update_log';
		
	}
	/**
	 * 修改前的设备列表
	 * @var list
	 */
	private $devList;
	
	public function beforeUpdate($idList){
		$idList =  (array)$idList;
		$list = $this->db->select('*')->from(MY_Model::T_DEV)->where_in('id',$idList)->get()->result_array();
		$list = array_combine(array_column($list, 'id'),$list);
		$this->devList = $list;
	}
	
	public function afterUpdate(){
		$idList = array_keys($this->devList);
		$afterList = $this->db->select('*')->from(MY_Model::T_DEV)->where_in('id',$idList)->get()->result_array();
		$afterList = array_combine(array_column($afterList, 'id'),$afterList);
		$this->load->model('Admin_mdl','admin');
		$admin = $this->admin->getUser() ;
		
		$item = array(
				'adminID'=>$admin['id'],
				'time'=>time(),
		);
		$logs = array();
		foreach ($this->devList as $devID=>$bfDev){
			$afData = isset($afterList[$devID]) ? $afterList[$devID] : null;
			$logs[] = array(
					'adminID'=>$admin['id'],
					'time'=>time(),
					'devID'=>$devID,
					'beforeData'=>json_encode($bfDev,JSON_UNESCAPED_UNICODE),
					'afterData'=>json_encode($afData,JSON_UNESCAPED_UNICODE),
			);
		}
		return $this->db->insert_batch($this->_table,$logs);
	}

	
	
	public function save($adminID,$idList,$schoolID){
		$idList = (array)$idList;
		$insert = array(
				'adminID'=>$adminID,
				'devIdList'=>implode(',', $idList),
				'afterSchoolID'=>$schoolID,
				'time'=>time(),
		);
		return $this->insert($insert);
	}
}

